using Newtonsoft.Json;
using System;
using System.Security.Cryptography;
using System.Text;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.ScheduleJob.SPI
{
    public class ScheduleJobRTPUtil
    {
        public static string GetMD5(object data)
        {
            if (data == null)
                return "";
            try
            {
                var md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(Encoding.Default.GetBytes(JsonConvert.SerializeObject(data)));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (System.Exception ex)
            {
                throw new FasException(ScheduleJobRTPExceptionCode.ScheduleJobExecuteError,string.Format(CommonStringClassSPI.ScheduleJobRTPUtil_InstanceIndetifierGenerateFail,ex.ToString()));
            }
        }
        public static string GetMD5(string content)
        {
            if (string.IsNullOrEmpty(content))
                return "";
            try
            {
                var md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(Encoding.Default.GetBytes(content));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (System.Exception ex)
            {
                throw new FasException(ScheduleJobRTPExceptionCode.ScheduleJobExecuteError, string.Format(CommonStringClassSPI.ScheduleJobRTPUtil_InstanceIndetifierGenerateFail, ex.ToString()));
            }
        }
        public static DateTime GetDefaultTime()
        {
            return Convert.ToDateTime("1900/1/1");
        }
    }
}
